//////////////////////////////////////////////////////////////////////////////
// Note: The code below is adapted from:                                    //
//  Crost, B., Felter, J., & Johnston, P. (2014).                           //
//  Aid under fire: Development projects and civil conflict.                //
//  American Economic Review, 104(6), 1833-56.                              //
//  Availabe at: https://www.aeaweb.org/articles?id=10.1257/aer.104.6.1833  //
//                                                                          //
////////////////////////////////////////////////////////////////////////////// 


save temp_for_nonparametric_graphs, replace

//////// Linear fit using triangular kernel

clear
set obs 1
gen n=1
save nonparametric_fits, replace

// calculate local linear fits for points below the threshold
foreach h in  -50 ///
-49	///
-48	///
-47	///
-46	///
-45	///
-44	///
-43	///
-42	///
-41	///
-40	///
-39	///
-38	///
-37	///
-36	///
-35	///
-34	///
-33	///
-32	///
-31	///
-30	///
-29	///
-28	///
-27	///
-26	///
-25	///
-24	///
-23	///
-22	///
-21	///
-20	///
-19	///
-18	///
-17	///
-16	///
-15	///
-14	///
-13	///
-12	///
-11	///
-10	///
-9	///
-8	///
-7	///
-6	///
-5	///
-4	///
-3	///
-2	///
-1	///
-.9	///
-.8	///
-.7	///
-.6	///
-.5	///
-.4	///
-.3	///
-.2	///
-.1	///
{
use temp_for_nonparametric_graphs, clear
keep if hhmemb==1
g u=.
g bw = 50
replace u=(dist-`h')/bw
g weight=1-abs(u)
replace weight=. if u<-1
replace weight=. if u>1

foreach i in $var {
reg `i' dist [pweight=weight] if dist<0
preserve
clear 
set obs 1
gen dist = `h'
predict `i'_xb
sleep 2000
capture append using nonparametric_fits
save nonparametric_fits, replace
restore
}

}

// calculate local linear fits for points above the threshold
foreach h in  .1 ///
.2	///
.3	///
.4	///
.5	///
.6	///
.7	///
.8	///
.9	///
1	///
2	///
3	///
4	///
5	///
6	///
7	///
8	///
9	///
10	///
11	///
12	///
13	///
14	///
15	///
16	///
17	///
18	///
19	///
20	///
21	///
22	///
23	///
24	///
25	///
26	///
27	///
28	///
29	///
30	///
31	///
32	///
33	///
34	///
35	///
36	///
37	///
38	///
39	///
40	///
41	///
42	///
43	///
44	///
45	///
46	///
47	///
48	///
49	///
50	///
{
use temp_for_nonparametric_graphs, clear
keep if hhmemb==1
g u=.
g bw = 50
replace u=(dist-`h')/bw
g weight=1-abs(u)
replace weight=. if u<-1
replace weight=. if u>1

foreach i in $var {

reg `i' dist [pweight=weight] if dist>=0
preserve
clear 
set obs 1
gen dist = `h'
predict `i'_xb
sleep 2000
capture append using nonparametric_fits
sleep 2000
save nonparametric_fits, replace
sleep 2000
restore

}
}

// collapse local linear fits by values of the running variable
sleep 2000
use  nonparametric_fits, clear
collapse ${var}_xb, by(dist)
sort dist
sleep 2000
save nonparametric_fits, replace



/////////////////////// generate quadratic or linear fits 
clear 
set obs 11
gen dist = _n*5
replace dist=0 if dist==55 
sort dist
save temp_for_quadratic_graphs, replace
sleep 2000

foreach i in $var {
use temp_for_nonparametric_graphs, clear
keep if hhmemb==1
reg `i' dist dist2 if dist<0   // star if linear fit is desired
*reg `i' dist if dist<0        // unstar if linear fit is desired
clear 
set obs 11
gen dist = -_n*5
replace dist=0 if dist==-55
gen dist2 = dist^2             // star if linear fit
predict `i'_xb20
sort dist

merge 1:1 dist using temp_for_quadratic_graphs
drop _merge
save temp_for_quadratic_graphs, replace
sleep 2000

use temp_for_nonparametric_graphs, clear
keep if hhmemb==1
reg `i' dist dist2 if dist>=0 // star if linear fit is desired
*reg `i' dist if dist>=0   // unstar if linear fit is desired
clear 
set obs 11
gen dist = _n*5
replace dist=0 if dist==55
gen dist2 = dist^2      // star if linear fit
predict `i'_xb21
sort dist

merge 1:1 dist using temp_for_quadratic_graphs
drop _merge
sort dist
sleep 2000
save temp_for_quadratic_graphs, replace
}




** generate scatter plots
use temp_for_nonparametric_graphs, clear
keep if hhmemb==1
g bin=.
replace bin=-45 if dist>=-50 & dist<-40 
replace bin=-35 if dist>=-40 & dist<-30
replace bin=-25 if dist>=-30 & dist<-20 
replace bin=-15 if dist>=-20 & dist<-10 
replace bin=-5 if dist>=-10 & dist<0 
replace bin=5 if dist>=0 & dist<10
replace bin=15 if dist>=10 & dist<20 
replace bin=25 if dist>=20 & dist<30 
replace bin=35 if dist>=30 & dist<40 
replace bin=45 if dist>=40

collapse ${var}* (count) n = $var, by(bin)

sort bin
save temp_bin, replace


use nonparametric_fits, clear
g bin=.
replace bin=-45 if dist>=-50 & dist<-40 
replace bin=-35 if dist>=-40 & dist<-30
replace bin=-25 if dist>=-30 & dist<-20 
replace bin=-15 if dist>=-20 & dist<-10 
replace bin=-5 if dist>=-10 & dist<0 
replace bin=5 if dist>=0 & dist<10
replace bin=15 if dist>=10 & dist<20 
replace bin=25 if dist>=20 & dist<30 
replace bin=35 if dist>=30 & dist<40 
replace bin=45 if dist>=40

sort dist
merge dist using temp_for_quadratic_graphs
tab _merge 
drop _merge

sort bin
merge bin using temp_bin
drop _merge
 
sort dist

twoway (scatter $var bin [aweight = n] ) /*
*/ (line  ${var}_xb dist if dist<=0, lpattern(solid)) /*
*/ (line ${var}_xb dist if dist>=0, lpattern(solid)) /*
*/ (line  ${var}_xb20 dist if dist<=0, lpattern(dash_dot)) /*
*/ (line ${var}_xb21 dist if dist>=0, lpattern(dash_dot)) /*
*/ , xtitle("Distance to cut-off (in meters)") ytitle("") /*
*/  legend(rows(1) order(1 "Mean" 2 "Nonparametric fit" 4 "Quadratic fit")) xline(0, lpattern(dash)) xlabel(-50[10]50)


graph save ${var}_RDgraph,  replace
*graph export "${var}_RDgraph.eps", replace
*graph export "${var}_RDgraph.pdf", replace

use temp_for_nonparametric_graphs, clear



